package com.smxy.mall.service.impl;

import com.smxy.mall.common.CustomException;
import com.smxy.mall.common.Response;
import com.smxy.mall.dao.ShopDao;
import com.smxy.mall.entity.Shop;
import com.smxy.mall.model.request.ShopReq;
import com.smxy.mall.service.ShopService;
import com.smxy.mall.utils.Common;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopDao shopDao;

    @Override
    public List<Shop> list() {
        return shopDao.findAll();
    }


    /**
     * 新增店铺
     * @param shopReq
     * @return
     */
    @Override
    public Shop create(ShopReq shopReq) {
        Shop shop = new Shop();
        shop.setManagerId(shopReq.getManagerId());
        shop.setManagerName(shopReq.getManagerName());
        shop.setShopName(shopReq.getShopName());
        shop.setPhone(shopReq.getPhone());
        shop.setTime(shopReq.getTime());
        shop.setAddress(shopReq.getAddress());
        shop.setLat(shopReq.getLat());
        shop.setLng(shopReq.getLng());
        shop.setStatus(2);  //2审核中
        return shopDao.save(shop);
    }

    /**
     * 编辑店铺
     * @param shopReq
     * @return
     */
    @Override
    public Shop save(ShopReq shopReq) {
        if(!Common.isId(shopReq.getId()+"")){
            throw new CustomException(Response.fail("店铺id不能为空"));
        }
        Shop myShop = shopDao.findById(shopReq.getId());
        myShop.setManagerId(shopReq.getManagerId());
        myShop.setManagerName(shopReq.getManagerName());
        myShop.setShopName(shopReq.getShopName());
        myShop.setPhone(shopReq.getPhone());
        myShop.setTime(shopReq.getTime());
        myShop.setAddress(shopReq.getAddress());
        myShop.setLat(shopReq.getLat());
        myShop.setLng(shopReq.getLng());
        return shopDao.save(myShop);
    }

    @Override
    public Shop info(Integer shopId) {
        return shopDao.findById(shopId);
    }

    @Override
    public Page<Shop> listPage(Pageable pageable) {
        return shopDao.findAll(pageable);
    }

    @Override
    public Page<Shop> listStatusPage(Integer status, Pageable pageable) {
        return shopDao.findByStatus(status,pageable);
    }

}
